//
//  ViewController.m
//  沙盒
//
//  Created by dllo on 16/3/29.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "ViewController.h"
#import "SandBoxHandler.h"
#import "PeopleModel.h"

@interface ViewController ()

@property (nonatomic, copy) NSString *pathOfArr;
@property (nonatomic, copy) NSString *pathOfString;
@property (nonatomic, copy) NSString *pathOfDic;
@property (nonatomic, copy) NSString *pathOfData;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleSandBox];
    [self saveFundationObject];
}

- (void)handleSandBox {
    SandBoxHandler *sand = [SandBoxHandler sanBoxHandler];
    NSLog(@"%@", sand.pathOfSandBox);
    NSLog(@"%@", sand.pathOfDocuments);
    NSLog(@"%@", sand.pathOfLibrary);
    
}

- (void)saveFundationObject {
    [self saveString];
    [self readStringFromDisk];
    
    [self saveDictionary];
    [self readDictionaryFromDisk];
    
    [self saveArray];
    [self readArrayFromDisk];
    
    [self saveData];
    [self readDataFromDisk];
    
    [self saveModel];
    [self readModelFromDisk];
    
    [self createManager];
    
    
#pragma mark - 作业3，每3秒存储并读取一次数据
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];
}

- (void)saveString {
    
    
    self.pathOfString = [SandBoxHandler saveInSanBoxHandler:Documents withFileName:@"str.txt"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager createFileAtPath:self.pathOfString contents:nil attributes:nil];
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:self.pathOfString];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(oneSecondsAction:) userInfo:@[fileHandle] repeats:YES];
    
}

- (void)oneSecondsAction:(NSTimer *)oneSecondTimer {
    static int count = 0;
    NSFileHandle *fileHandle = [(NSArray *)oneSecondTimer.userInfo lastObject];
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter  = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy/hh/mm hh : kk : dd"];
    NSString *currentTime = [formatter stringFromDate:date];
    NSLog(@"1111111111111111%@", currentTime);
    currentTime = [currentTime stringByAppendingString:@"\n"];
    NSData *data = [currentTime dataUsingEncoding:NSUTF8StringEncoding];
    [fileHandle writeData:data];
    count++;
    if (count == 60) {
        [fileHandle closeFile];
        [oneSecondTimer invalidate];
    }
}

- (void)readStringFromDisk {
    NSString *str = [NSString stringWithContentsOfFile:self.pathOfString encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", str);
}

- (void)saveDictionary {
    NSDictionary *dic = @{@"name":@"zhangsan", @"age":@"18", @"address":@"Dalian"};
    self.pathOfDic = [SandBoxHandler saveInSanBoxHandler:Documents withFileName:@"dic.plist"];
    [dic writeToFile:self.pathOfDic atomically:YES];
    
}

- (void)readDictionaryFromDisk {
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:self.pathOfDic];
    NSLog(@"%@", dic);
}

- (void)saveArray {
    NSArray *arr = @[@"zhangsan", @"lisi", @"wangwu", @"zhuliu"];
    self.pathOfArr = [SandBoxHandler saveInSanBoxHandler:Documents withFileName:@"arr.plist"];
    [arr writeToFile:self.pathOfArr atomically:YES];
}

- (void)readArrayFromDisk {
    NSArray *arr = [NSArray arrayWithContentsOfFile:self.pathOfArr];
    NSLog(@"%@", arr);
}

- (void)saveData {
    UIImage *im = [UIImage imageNamed:@"2.jpg"];
    NSData *data = UIImageJPEGRepresentation(im, 1);
    [data writeToFile:[SandBoxHandler saveInSanBoxHandler:Documents withFileName:@"image.jpeg"] atomically:YES];
}

- (void)readDataFromDisk {
    NSData *data = [NSData dataWithContentsOfFile:[SandBoxHandler saveInSanBoxHandler:Documents withFileName:@"image.jpeg"]];
    UIImage *ima = [UIImage imageWithData:data];
    NSLog(@"%@", ima);
}

- (void)saveModel {
    PeopleModel *person = [[PeopleModel alloc] init];
    person.name = @"liming";
    person.age = 12;
    
    NSMutableData *mData = [[NSMutableData alloc]init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
    [archiver encodeObject:person forKey:@"people"];
    [archiver finishEncoding];
    
    [mData writeToFile:[SandBoxHandler saveInSanBoxHandler:Documents withFileName:@"people.plist"] atomically:YES];
    
}

- (void)readModelFromDisk {
    
    NSData *data = [NSData dataWithContentsOfFile:[SandBoxHandler saveInSanBoxHandler:Documents withFileName:@"people.plist"]];
    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];
    PeopleModel *model = [unArchiver decodeObjectForKey:@"people"];
    [unArchiver finishDecoding];
    NSLog(@"%@ %ld", model.name, model.age);
    
}

- (void)timeAction:(NSTimer *)timeAction {
    
    
    [self saveArrWithPeopleModel];
    [self readArrWithPeopleModelFromDisk];
}

#pragma mark - 作业1， 存储联系人数组
- (void)saveArrWithPeopleModel {
    NSArray *peopleArr = @[@{@"name":@"lihua",@"age":@"10"}, @{@"name":@"zhaotian", @"age":@"20"}, @{@"name":@"taoyuanming", @"age":@"60"}];
    NSMutableData *arrMutableData = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:arrMutableData];
    [archiver encodeObject:peopleArr forKey:@"peopleArr"];
    [archiver finishEncoding];
    [arrMutableData writeToFile:[SandBoxHandler saveInSanBoxHandler:Documents withFileName:@"peopleArrModel.plist"] atomically:YES];
    
}

- (void)readArrWithPeopleModelFromDisk {
    NSData *data = [NSData dataWithContentsOfFile:[SandBoxHandler saveInSanBoxHandler:Documents withFileName:@"peopleArrModel.plist"]];
    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    NSArray *peopleArr = [unArchiver decodeObjectForKey:@"peopleArr"];
    [unArchiver finishDecoding];
    NSLog(@"%@", peopleArr);
}

#pragma mark - 作业2，在document下创建文件夹
- (void)createManager {
    /** 文件夹路径 */
    NSString *str = [[SandBoxHandler pathOfDocuments] stringByAppendingPathComponent:@"manager"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    /** 在str路径下创建文件夹 */
    [fileManager createDirectoryAtPath:str withIntermediateDirectories:YES attributes:nil error:nil];
   /** test.txt路径 */
    NSString *path = [str stringByAppendingPathComponent:@"test"];
    UIImage *imag = [UIImage imageNamed:@"2.jpg"];
    NSData *data = UIImageJPEGRepresentation(imag, 1);
    /** 在path路径下创建text.tex文件 */
    [fileManager createFileAtPath:path contents:data attributes:nil];
    NSLog(@"%@", path);
    
    /** temp文件夹下testDirectory文件夹路径 */
    SandBoxHandler *sand = [SandBoxHandler sanBoxHandler];
    
    NSString *temp = [sand.pathOfTemp stringByAppendingPathComponent:@"testDirectory"];
    NSFileManager *testManager = [NSFileManager defaultManager];
    NSString *path2 = [temp stringByAppendingPathComponent:@"testDirectory"];
    /** 创建文件夹 */
    [testManager createDirectoryAtPath:temp withIntermediateDirectories:YES attributes:nil error:nil];
    [fileManager copyItemAtPath:path toPath:path2 error:nil];
    NSLog(@"%@", path2);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
